/*
 * @Author: szx
 * @Date: 2022-03-17 10:40:20
 * @LastEditTime: 2022-03-17 10:42:54
 * @Description:
 * @FilePath: \leetcode\700-799\720\720.js
 */
/**
 * @param {string[]} words
 * @return {string}
 */
var longestWord = function (words) {
    words.sort();
    let longest = '';
    let set = new Set();
    set.add('');
    const n = words.length;
    for (let i = 0; i < n; i++) {
        const word = words[i];
        if (set.has(word.slice(0, word.length - 1))) {
            set.add(word);
            longest = word;
        }
    }
    return longest;
};
